package com.ysd.lis.service.sta.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.dto.sta.StaModelDto;
import com.ysd.lis.dto.sta.StaModelParam;
import com.ysd.lis.entity.sta.StaMenu;
import com.ysd.lis.entity.sta.StaModelMenu;
import com.ysd.lis.mapper.sta.StaModelMenuMapper;
import com.ysd.lis.service.sta.StaModelMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * cold_chain.sys_Model 服务实现类
 * </p>
 *
 * @author prs
 * @since 2024-12-27
 */
@Service
public class StaModelMenuServiceImpl extends ServiceImpl<StaModelMenuMapper, StaModelMenu> implements StaModelMenuService {

    @Autowired
    StaModelMenuMapper staModelMenuMapper;
    @Override
    public Result findModelMenuList(StaModelParam param) {
        MPJLambdaWrapper<StaModelMenu> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(StaModelMenu.class)
                .selectAs("ld", StaMenu::getUrl,StaModelMenu::getUrl)
                .leftJoin(StaMenu.class,"ld",StaMenu::getId,StaModelMenu::getStaMenuId)
                .eq(StaModelMenu::getDelFlag,0);
        if(StringUtils.isNotBlank(param.getModelId())){
            queryWrapper.eq(StaModelMenu::getModelId, param.getModelId());
        }
        if(StringUtils.isNotBlank(param.getStaModId())){
            queryWrapper.eq(StaModelMenu::getStaModId, param.getStaModId());
        }
        queryWrapper.orderByAsc(StaModelMenu::getSeq);
        List<StaModelMenu> menuList = staModelMenuMapper.selectList(queryWrapper);
        List<StaModelMenu> returnList = new ArrayList<>();
        menuList.forEach(a -> {
            if (StringUtils.isBlank(a.getParentId())) {
                //a.put("children", this.getChildren(a, menuList));
                a.setChildren(this.getChildren(a, menuList));
                returnList.add(a);
            }
        });
        return Result.succ(1, "成功", returnList);
    }

    @Override
    public Result batchDeleteModelMenuList(StaModelDto dto) {
        List<StaModelMenu> supplierList = dto.getStaModelMenuList();
        List<String> ids = supplierList.stream().map(StaModelMenu::getId).collect(Collectors.toList());

        boolean b = this.removeByIds(ids);
        if(b){
            return Result.succ(1, "删除成功", null);
        }else {
            return Result.fail(400,"删除失败","");
        }
    }

    @Override
    public Result batchAddModelMenuList(StaModelDto dto) {
        List<StaModelMenu> staModelMenuList = dto.getStaModelMenuList();
        boolean b = this.saveBatch(staModelMenuList);
        if(b){
            return Result.succ(1, "添加成功", null);
        }else {
            return Result.fail(400,"添加失败","");
        }
    }

    @Override
    public Result addModelMenu(StaModelMenu staModel) {
        int insert = staModelMenuMapper.insert(staModel);
        if(insert>0){
            return Result.succ(1,"添加成功",staModel);
        }else {
            return Result.fail(400,"添加失败","");
        }
    }

    @Override
    public Result editModelMenu(StaModelMenu supplier) {
        int updateById = staModelMenuMapper.updateById(supplier);
        if(updateById>0){
            return Result.succ(1,"提交成功",supplier);
        }else {
            return Result.fail(400,"修改失败","");
        }
    }

    public List<StaModelMenu> getChildren(StaModelMenu a, List<StaModelMenu> menuList) {
        List<StaModelMenu> result = new ArrayList();
        for (StaModelMenu menu : menuList) {
            if (StringUtils.isBlank(menu.getParentId())) {
                menu.setParentName("");
            }

            if (StringUtils.isNotBlank( menu.getParentId()) && menu.getParentId().equals(a.getId())) {
                menu.setParentName(a.getMenuTitle());
                result.add(menu);
            }

        }
        for (StaModelMenu menu : result) {
            menu.setChildren(getChildren(menu, menuList));
        }
        if (result.size() == 0) {
            return new ArrayList<>();
        }
        return result;

    }
}
